package de.dhe.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import com.fasterxml.jackson.databind.ObjectMapper;

import de.dhe.client.domain.Response;

public class LoginCtrl extends AbstractCltr {

	private static final String REGISTER = "register";
	private static final String LOGIN = "login";

	public LoginCtrl(HttpClient client) {
		super(client);
	}

	public void createNewUser(String name, String mail, String password) {
		HttpPost post = new HttpPost(REST_URL);
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			nameValuePairs.add(new BasicNameValuePair("tag", REGISTER));

			nameValuePairs.add(new BasicNameValuePair("name", name));
			nameValuePairs.add(new BasicNameValuePair("email", mail));
			nameValuePairs.add(new BasicNameValuePair("password", password));

			post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = client.execute(post);
			int statusCode = response.getStatusLine().getStatusCode();

			System.out.println("Response statsu" + statusCode);
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			String line = "";
			StringBuffer bu = new StringBuffer();
			while ((line = rd.readLine()) != null) {
				System.out.println(line);
				bu.append(line);
			}

			ObjectMapper mapper = new ObjectMapper();
			Response json = mapper.readValue(bu.toString(), Response.class);

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public Response login(String email, String password) {
		HttpPost post = new HttpPost(REST_URL);
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			nameValuePairs.add(new BasicNameValuePair("tag", LOGIN));

			nameValuePairs.add(new BasicNameValuePair("email", email));
			nameValuePairs.add(new BasicNameValuePair("password", password));

			post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = client.execute(post);
			int statusCode = response.getStatusLine().getStatusCode();

			System.out.println("Response statsu" + statusCode);
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			String line = "";
			StringBuffer bu = new StringBuffer();
			while ((line = rd.readLine()) != null) {
				System.out.println(line);
				bu.append(line);
			}

			ObjectMapper mapper = new ObjectMapper();
			Response json = mapper.readValue(bu.toString(), Response.class);

			return json;

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}

}
